{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style=\"text-align:center\">Advanced Querying or Periodic Table Quiz</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "from mendeleev import Element, IonizationEnergy, OxidationState\n",
    "from mendeleev.db import get_session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session = get_session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ans = session.query(Element.name).filter(Element.atomic_number == 8).all()\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ans = session.query(Element.name).filter(Element.atomic_number > 100).all()\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get all the elements order with respect to increasing covalent radius"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ans = (\n",
    "    session.query(Element)\n",
    "    .filter(Element.covalent_radius is not None)\n",
    "    .order_by(Element.covalent_radius)\n",
    ")\n",
    "for a in ans:\n",
    "    print(\"{:3s} {:15s} {:>6.2f}\".format(a.symbol, a.name, a.covalent_radius))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* largest, smallest, (min, max)\n",
    "* how many (count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get all the elements whose first ionization energy is smaller that 5 eV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ans = (\n",
    "    session.query(Element)\n",
    "    .join(IonizationEnergy)\n",
    "    .filter(IonizationEnergy.degree == 1)\n",
    "    .filter(IonizationEnergy.energy < 5)\n",
    "    .all()\n",
    ")\n",
    "for a in ans:\n",
    "    print(a.symbol, a.ionenergies[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ans = (\n",
    "    session.query(Element)\n",
    "    .join(OxidationState)\n",
    "    .filter(OxidationState.oxidation_state == 1)\n",
    "    .all()\n",
    ")\n",
    "for a in ans:\n",
    "    print(a.symbol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
